package com.yvon.boot.common.validation.validator;

import com.yvon.boot.common.validation.annotation.Flag;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 状态标记校验器.
 *
 * @author : Yvon
 * @since : 2021-06-22
 */
public class FlagValidator implements ConstraintValidator<Flag,Object> {
    private String[] values;
    @Override
    public void initialize(Flag flag) {
        this.values = flag.value();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        boolean isValid = false;
        if(value==null){
            //当状态为空时使用默认值
            return true;
        }
        for (String s : values) {
            if (s.equals(String.valueOf(value))) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }
}
